package com.yy.wga.common.util.common;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;
import static java.util.regex.Pattern.matches;

/**
 * 数据校验
 *
 * @author <a href="mailto:ludezh@dingtalk.com"> ludezh </a>
 * @version 1.0.0   2018/12/24,20:15
 * @since 1.0.0     2018/12/24,20:15
 */
public class DataUtils {
    /**
     * 正则表达式：验证用户名
     */
    public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,20}$";
    /**
     * 正则表达式：验证密码
     */
    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$";
    /**
     * 正则表达式：验证手机号
     */
    public static final String REGEX_MOBILE = "^1([3-9][0-9])\\d{8}$";
    /**
     * 正则表达式：验证邮箱
     */
    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    /**
     * 正则表达式：验证汉字
     */
    public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";
    /**
     * 正则表达式：验证身份证
     */
    public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";
    /**
     * 正则表达式：验证URL
     */
    public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
    /**
     * 正则表达式：验证IP地址
     */
    public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
    /**
     * 正则表达式: 验证微信号
     */
    public static final String WEIXIN_CODE = "^[a-zA-Z]{1}[-_a-zA-Z0-9]{5,19}+$";
    /**
     * 数字+全字母
     */
    public static final String DIGITAL_ALL_LETTER = "^[A-Za-z0-9]+$";
    public static final String DIGITAL_ALL_LETTER_CONTAIN = "[A-Za-z0-9]";
    /**
     * 邀请码
     */
    public static final String INVITE_CODE = "^[A-Za-z0-9]{6,10}+$";
    /**
     * 昵称正则表达式
     */
    public static final String NICK_NAME = "^[\\w\\u4e00-\\u9fa5]{1,10}$";
    /**
     * Unicode编码
     */
    public static final String UNION_CODE = "(\\\\u(\\p{XDigit}{4}))";
    /**
     * 特殊字符正则表达式
     */
    public static final String SPECIAL_CHAR = "[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~！@#￥%……&*（）——+|{}《》【】‘；：”“’。，、？]";

    /**
     * 所有EMOJI表情符号正则
     */
    public static final String EMOJI = "(?:[\uD83C\uDF00-\uD83D\uDDFF]|[\uD83E\uDD00-\uD83E\uDDFF]|[\uD83D\uDE00-\uD83D\uDE4F]|[\uD83D\uDE80-\uD83D\uDEFF]|[\u2600-\u26FF]\uFE0F?|[\u2700-\u27BF]\uFE0F?|\u24C2\uFE0F?|[\uD83C\uDDE6-\uD83C\uDDFF]{1,2}|[\uD83C\uDD70\uD83C\uDD71\uD83C\uDD7E\uD83C\uDD7F\uD83C\uDD8E\uD83C\uDD91-\uD83C\uDD9A]\uFE0F?|[\u0023\u002A\u0030-\u0039]\uFE0F?\u20E3|[\u2194-\u2199\u21A9-\u21AA]\uFE0F?|[\u2B05-\u2B07\u2B1B\u2B1C\u2B50\u2B55]\uFE0F?|[\u2934\u2935]\uFE0F?|[\u3030\u303D]\uFE0F?|[\u3297\u3299]\uFE0F?|[\uD83C\uDE01\uD83C\uDE02\uD83C\uDE1A\uD83C\uDE2F\uD83C\uDE32-\uD83C\uDE3A\uD83C\uDE50\uD83C\uDE51]\uFE0F?|[\u203C\u2049]\uFE0F?|[\u25AA\u25AB\u25B6\u25C0\u25FB-\u25FE]\uFE0F?|[\u00A9\u00AE]\uFE0F?|[\u2122\u2139]\uFE0F?|[\u231A\u231B\u2328\u23CF\u23E9-\u23F3\u23F8-\u23FA]\uFE0F?)";

    /**
     * 金额正则表达式
     */
    public static final String MONEY = "^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){1,2})?$";

    /**
     * 图片正则
     */
    public static final String PIC = ".+(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif|.BMP|.bmp|.PNG|.png)$";

    /**
     * 数字开头正则
     */
    public static final String START_NUM = "[0-9]*";

    /**
     * 判断字符串是不是以数字开头
     *
     * @param str
     * @return boolean
     * @author ludezh
     * @date 2020/9/21 10:42
     * @version 1.0.0
     */
    public static boolean isStartNum(String str) {
        Pattern pattern = Pattern.compile(START_NUM);
        Matcher isNum = pattern.matcher(str.charAt(0) + "");
        return isNum.matches();
    }

    /**
     * 校验是否为图片
     *
     * @param pic
     * @return
     */
    public static boolean isPic(String pic) {
        return matches(PIC, pic);
    }

    /**
     * 校验金额
     *
     * @param money
     * @return
     */
    public static boolean isMoney(String money) {
        // 判断小数点后2位的数字的正则表达式
        return matches(MONEY, money);
    }

    /**
     * 过滤emoji表情
     *
     * @param input
     * @return
     */
    public static String replaceEmoji(String input) {
        return compile(EMOJI).matcher(input).replaceAll("");
    }

    /**
     * 校验emoji表情
     *
     * @param input
     * @return
     */
    public static boolean isEmoji(String input) {
        return matches(EMOJI, input);
    }

    /**
     * 过滤特殊字符
     *
     * @param input
     * @return
     */
    public static String replaceSpecialChar(String input) {
        return compile(SPECIAL_CHAR).matcher(input).replaceAll("");
    }

    /**
     * 校验特殊字符串
     *
     * @param input
     * @return
     */
    public static boolean isSpecialChar(String input) {
        return matches(SPECIAL_CHAR, input);
    }

    /**
     * Unicode转字符串
     *
     * @param str
     * @return
     */
    public static String unicodeToStr(String str) {
        Matcher matcher = compile(UNION_CODE).matcher(str);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            str = str.replace(matcher.group(1), ch + "");
        }
        return str;
    }

    /**
     * 字符串转Unicode
     *
     * @param s
     * @return
     */
    public static String strToUnicode(String s) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            if (ch > 255) {
                str.append("\\u").append(Integer.toHexString(ch));
            } else {
                str.append("\\u00").append(Integer.toHexString(ch));
            }
        }
        return str.toString();
    }

    /**
     * 校验昵称
     *
     * @param nickName
     * @return
     */
    public static boolean isNickName(String nickName) {
        return matches(NICK_NAME, nickName);
    }

    /**
     * 校验邀请码
     *
     * @param inviteCode
     * @return
     */
    public static boolean isInviteCode(String inviteCode) {
        return Pattern.matches(INVITE_CODE, inviteCode);
    }

    /**
     * 校验数字+全字母
     *
     * @param str
     * @return
     */
    public static boolean isDigitalAllLetter(String str) {
        return Pattern.matches(DIGITAL_ALL_LETTER, str);
    }

    /**
     * 校验是否包含数字+全字母
     *
     * @param str
     * @return
     */
    public static boolean isContainDigitalAllLetter(String str) {
        Pattern p = Pattern.compile(DIGITAL_ALL_LETTER_CONTAIN);
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    /**
     * 校验微信号
     *
     * @param wxCode
     * @return
     */
    public static boolean isWxCode(String wxCode) {
        return matches(WEIXIN_CODE, wxCode);
    }

    /**
     * 校验用户名
     *
     * @param username
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isUsername(String username) {
        return matches(REGEX_USERNAME, username);
    }

    /**
     * 校验密码
     *
     * @param password
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isPassword(String password) {
        return matches(REGEX_PASSWORD, password);
    }

    /**
     * 校验手机号
     *
     * @param mobile
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isMobile(String mobile) {
        return matches(REGEX_MOBILE, mobile);
    }

    /**
     * 校验邮箱
     *
     * @param email
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isEmail(String email) {
        return matches(REGEX_EMAIL, email);
    }

    /**
     * 校验汉字
     *
     * @param chinese
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isChinese(String chinese) {
        return matches(REGEX_CHINESE, chinese);
    }

    /**
     * 校验身份证
     *
     * @param idCard
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isIDCard(String idCard) {
        return matches(REGEX_ID_CARD, idCard);
    }

    /**
     * 校验URL
     *
     * @param url
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isUrl(String url) {
        return matches(REGEX_URL, url);
    }

    /**
     * 校验IP地址
     *
     * @param ipAddr
     * @return
     */
    public static boolean isIPAddr(String ipAddr) {
        return matches(REGEX_IP_ADDR, ipAddr);
    }

    /**
     * 团队(会员数量相关)数字格式化
     * 数量大于1百万的 转成格式 xx.xx万 如100.23万
     *
     * @param num
     * @return
     */
    public static String teamNumIntFormat(Integer num) {
        if (num >= 10000) {
            double w = num / 10000.0;
            if (w >= 1) {
                return new BigDecimal(w).setScale(1, RoundingMode.UP) + "万";
            }
        }
        return num.toString();
    }

    /**
     * 过滤非数字
     *
     * @param str
     * @return
     */
    public static String getNumeric(String str) {
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    /**
     * 过滤html标签
     *
     * @param htmlStr
     * @return
     */
    public static String delHTMLTag(String htmlStr) {
        //定义script的正则表达式
        String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>";
        //定义style的正则表达式
        String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>";
        //定义HTML标签的正则表达式
        String regEx_html = "<[^>]+>";
        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        //过滤script标签
        htmlStr = m_script.replaceAll("");
        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        //过滤style标签
        htmlStr = m_style.replaceAll("");
        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        //过滤html标签
        htmlStr = m_html.replaceAll("");
        //返回文本字符串
        return htmlStr.trim();
    }
}